<?php
  include __DIR__."/config/cors.php";
  include __DIR__."/database.php";


  $username = @$_GET['username'];
  $role = @$_GET['role'];
  $nid = @$_GET['nid'];
  $newImg = @$_GET['newImg'];
  $title = @$_GET['title'];
  $context = @$_GET['context'];

  if(!isset($username) or !isset($role)){
    echo json_encode([
        "code" => 202,
        "msg"=> "请管理员登陆后在修改"
      ]);
      exit;
  }

  if(!isset($title) or !isset($context) or !isset($nid)){
    echo json_encode([
        "code" => 203,
        "msg"=> "必要参数传递有误"
      ]);
      exit;
  }

   $db = new DB();
   $sql = "select * from user where username = '$username'";
   $data = $db->selectOne($sql);
   if (is_array($data) and count($data) > 0) {
      $uid = $data['id'];
      $sql = "update news set title = '$title',context = '$context',newImg = '$newImg' where id = '$nid' and uid = '$uid'";
      if ($db->insert($sql)) {
        echo json_encode([
            "code" => 200,
            "msg"=>"修改成功"
        ]);
      } else {
        echo json_encode([
            "code" => 204,
            "msg"=> "修改失败: " . $data
        ]);
    }
}else {
    is_array($data) ? $msg = "请登陆后在修改" : $msg = "修改失败: ".$data;
    echo json_encode([
       "code" => 201,
       "msg"=> $msg
     ]);
   }
?>